package dao;

import com.alibaba.fastjson.JSON;
import pojo.equip.EquipMsgEntity;
import pojo.user.UserEntity;
import utils.DataFileName;
import utils.DataUtils;
import utils.JsonUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class UserDao {
    public void saveUser(UserEntity user) {
        String json = JSON.toJSONString(user);
        try {
            DataUtils.writeData(DataFileName.USER.toString(), json);
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
    }

    public List<UserEntity> findUserList() throws IOException {
        String json = DataUtils.readData(DataFileName.USER.getName());
        if(json.length() == 0){
            return null;
        }
        List<UserEntity> userEntityList = new ArrayList<>();
        String[] users = json.split("/");
        for(String str:users){
            UserEntity user = JsonUtil.jsonToObject(str,UserEntity.class);
            userEntityList.add(user);
        }
        return userEntityList;
    }

    public UserEntity findUser(String account) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){
                if(account.equals(user.getAccount())){
                   return user;
                }
            }
        }
        return null;
    }

    public UserEntity findUserById(String id) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){
                if(id.equals(user.getId())){
                    return user;
                }
            }
        }
        return null;
    }

    public List<UserEntity> findUserByName(String name)throws IOException{
        List<UserEntity> userEntityList = findUserList();
        List<UserEntity> userEntityList1 = new ArrayList<>();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){
                if(name.equals(user.getName())){
                    userEntityList1.add(user);
                }
            }
            return userEntityList1;
        }
        return null;

    }

    public List<UserEntity> findUserByPower(String name)throws IOException{
        List<UserEntity> userEntityList = findUserList();
        List<UserEntity> userEntityList1 = new ArrayList<>();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){
                if(name.equals(user.getPower())){
                    userEntityList1.add(user);
                }
            }
            return userEntityList1;
        }
        return null;

    }

    public void deleteUser(String id) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        userEntityList.removeIf(user -> user.getId().equals(id));
        DataUtils.clearData(DataFileName.USER.toString());
        for(UserEntity user:userEntityList){
            this.saveUser(user);
        }

    }

    public String getMaxId() throws IOException {
        List<UserEntity> userEntityList = findUserList();
        int max = 0;
        if (userEntityList == null || userEntityList.size() == 0) {
            return "1";
        } else {
            for (UserEntity userEntity : userEntityList) {
                if (Integer.parseInt(userEntity.getId()) > max) {
                    max = Integer.parseInt(userEntity.getId());
                }
            }
            return String.valueOf(max + 1);
        }
    }
    public boolean isUser(String LoginAccount) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){
                if(LoginAccount.equals(user.getAccount())){
                    return true;
                }
            }
        }
        return false;
    }

    public UserEntity searchFactoryName(String factoryName) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        if(userEntityList!=null){
            for(UserEntity user:userEntityList){
                if(factoryName.equals(user.getFactoryName())){
                    return user;
                }
            }
        }
        return null;
    }

    public void changeState(UserEntity userEntity) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        for(UserEntity user: userEntityList){
            if(user.getId().equals(userEntity.getId())){
                if(userEntity.getState().equals("关停")){
                    user.setState("使用中");
                }else {
                    user.setState("关停");
                }
            }
        }
        DataUtils.clearData(DataFileName.USER.toString());
        for(UserEntity user:userEntityList){
            this.saveUser(user);
        }
    }
    public void addEquip(String id,EquipMsgEntity equipMsgEntity) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        if(findUserList()==null) return;
        for(UserEntity user:userEntityList){
            if(user.getId().equals(id))
                user.getRentList().add(equipMsgEntity);
        }
        DataUtils.clearData(DataFileName.USER.toString());
        for(UserEntity user:userEntityList){
            this.saveUser(user);
        }
    }
    public void removeEquip(String factory,EquipMsgEntity equipMsgEntity) throws IOException {
        List<UserEntity> userEntityList = findUserList();
        if(findUserList()==null) return;
        for(UserEntity user:userEntityList){
            if(user.getFactoryName().equals(factory)){
               List<EquipMsgEntity> equips=user.getRentList();
               equips.removeIf(equipMsgEntity1 -> equipMsgEntity1.getId().equals(equipMsgEntity.getId()));
               user.setRentList(equips);
            }

        }
        DataUtils.clearData(DataFileName.USER.toString());
        for(UserEntity user:userEntityList){
            this.saveUser(user);
        }
    }
}

